/**
 * Created by apple on 2018/2/23.
 */
requireJS(['constant', 'site'], function (constant, site) {

    var url = {
        checkCaptcha: "checkCaptcha.do",
        checkPhoneCaptcha: "checkPhoneCaptcha.do",
        sendmsg: "sendmsg.do",
        resetPassword: "resetPassword.do"
    }

    var a = new Date();
    $(".year").html(a.getFullYear())

    var form = {};

    //请求图片验证码
    function getCode() {
        site.send({
            url: 'imageCode.do',
            callback: function (data) {
                if (data && data.content) {
                    $('.codeImg').attr('src', data.content.img);
                    form.token = data.content.token;
                }
            }
        });
    }

    //第一次进入获取验证码
    getCode();

    function login() {
        constant.fn.formParam('#login-form', form);
        /*if (!/^\s*$/.test(form.username)) {
            constant.fn.Message('I', '用户名不能为空');
            return
        }*/
        if (!form.username) {
            constant.fn.Message('I', '用户名不能为空');
            return
        }
        if (!form.password) {
            constant.fn.Message('I', '密码不能为空');
            return
        }
        if (!form.changeCode) {
            constant.fn.Message('I', '验证码不能为空');
            return
        }
        form.type = 1;
        var _path = 'manager/adminLogin.do' 	 //后台请求接口
        site.send({
            url: _path,
            data: form,
            callback: function (data) {
                data = JSON.parse(data);
                if (data && data.type > 0) {
                    /*socket.onopen = function(){
                    }
                    socket.onmessage = function(e){
                    }
                    socket.onclose = function () {
                        alert("连接已关闭...");
                    };*/

                    /*将hbhr设置为本地存储*/
                    var content = JSON.stringify(data.content);
                    localStorage.setItem('content', content);
                    login_success();

                } else {
                    constant.fn.Message('F', data.msg);
                    if(data.msg == '验证码已过期'){
                        getCode();
                    }
                    //getCode();
                }
            }
        });
    }

    //获取验证码
    $('.getCode').on('click', function (e) {
        //e.preventDefault();
        getCode();
        return false;
    })
    //登录按钮操作
    $('#login-btn').on('click', function () {
        login()
    })

    //按enter键登录
    document.onkeydown = function (event) {
        var e = event || window.event || arguments.callee.caller.arguments[0];
        //e.stopPropagation();
        if (e && e.keyCode == 13) {
            if ($(".logIn-clude").css('display') != 'none') {
                login()
            }
            else if ($(".find_psw").css('display') != 'none') {
                findPsw();
            }
            else if ($(".enter_psw").css('display') != 'none') {
                enterPsw();
            }
            else if ($(".reset_psw").css('display') != 'none') {
                resetPsw();
            }
            return false;
        }
    }

    function login_success() {
        $(".logIn-clude").hide();
        $(".login_success").show();
        $(".find_psw").hide();
        var content = JSON.parse(localStorage.getItem('content'));
        var entry;
        if (JSON.parse(localStorage.getItem('content')).menus[0]) {
            if (JSON.parse(localStorage.getItem('content')).menus[0].children[0]) {
                //entry = JSON.parse(localStorage.getItem('content')).menus[0].children[0].url;
                if (JSON.parse(localStorage.getItem('content')).menus[0].children[0].children[0]) {
                    entry = JSON.parse(localStorage.getItem('content')).menus[0].children[0].children[0].url;
                } else {
                    entry = JSON.parse(localStorage.getItem('content')).menus[0].children[0].url;
                }
            } else {
                entry = JSON.parse(localStorage.getItem('content')).menus[0].url;
            }
        }
        else {
            constant.fn.Message("F", "该用户无菜单");
            return;
        }

        $(".user").text(content.admin.username)
        var i = 3;
        $(function () {
            setTimeout(function () {
                window.location.href = document.getElementsByTagName("base")[0].getAttribute("href") + entry;
            }, 3000);//5秒后返回首页
            after();
        });

        //自动刷新页面上的时间
        function after() {
            $(".intervalSecond").empty().append(i);
            if (i > 1) {
                i = i - 1;
            }
            setTimeout(function () {
                after();
            }, 1000);
        }

    }

    $(".logIn-a-forget").click(function () {
        getCode();
        $(".find_psw").show();
        $(".login_success").hide();
        $(".logIn-clude").hide();
        return false;


    })
    $(".logIn-a-remember").click(function () {
        getCode();
        $(".find_psw").hide();
        $(".login_success").hide();
        $(".logIn-clude").show();

        return false;
    })

    function findPsw() {
        constant.fn.formParam('#findPsw_from', form);
        if (!form.pswphonenum) {
            constant.fn.Message('I', '手机号不能为空');
            return
        }
        if (!constant.reg.phone.test(form.pswphonenum)) {
            constant.fn.Message('I', '请输入正确的手机号码');
            return
        }
        constant.fn.formParam('#code-from', form);
        if (!form.findchangeCode) {
            constant.fn.Message('I', '验证码不能为空');
            return
        }

        site.send({
            url: url.checkCaptcha,
            data: {
                changeCode: form.findchangeCode,
                phone: form.pswphonenum,
                token: form.token,
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    constant.fn.Message('T', data.msg)
                    site.send({
                        url: url.sendmsg,
                        data: {
                            phone: form.pswphonenum
                        },
                        callback: function (data) {
                            data = JSON.parse(data);
                            if (data.type == '1') {
                                $(".find_psw").hide();
                                /*$(".login_success").hide();
                                $(".logIn-clude").hide();*/
                                $(".enterPsw-phoneNum").val(form.pswphonenum)
                                $(".enter_psw").show();

                                var time = $(".spanParent > ._span");
                                a = setInterval(function () {
                                    if (parseInt(time.text()) > 1) {
                                        time.text(parseInt(time.text()) - 1);
                                    }
                                    else {
                                        clearInterval(a);
                                    }
                                }, 1000)


                                setTimeout(function () {
                                    $(".spanParent").hide();
                                    $(".btnGet").show();
                                }, 60000)

                            }
                            else {
                                constant.fn.Message('F', data.msg);
                            }
                        }
                    })
                }
                else {
                    constant.fn.Message("F", data.msg)
                }
            }
        })
    }

    $(".findPswFun").click(function () {
        findPsw();
//找回密码调用
    })


    function btnGet() {
        var phone = $(".enterPsw-phoneNum").val();
        var captcha = $(".enterPsw-inputCode").val();
        /*if (!captcha) {
            constant.fn.Message('I', "请输入手机接收到的验证码")
            return;
        }*/
        site.send({
            url: url.sendmsg,
            data: {
                phone: form.pswphonenum
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    //constant.fn.Message('T',data.msg)
                    $(".spanParent").show();
                    $(".btnGet").hide();
                    var time = $(".spanParent > ._span");
                    time.text(60);
                    a = setInterval(function () {
                        if (parseInt(time.text()) > 1) {
                            time.text(parseInt(time.text()) - 1);
                        }
                        else {
                            clearInterval(a);
                        }
                    }, 1000)

                    setTimeout(function () {
                        $(".spanParent").hide();
                        $(".btnGet").show();
                    }, 60000)

                } else {
                    constant.fn.Message('F', data.msg);
                }
            }
        })
    }

    $(".btnGet").on('click', function () {
        //发送验证码
        btnGet();
    })

    function enterPsw() {
        var phone = $(".enterPsw-phoneNum").val();
        var captcha = $(".enterPsw-inputCode").val();
        if (!captcha) {
            constant.fn.Message('I', "请输入手机接收到的验证码")
        }
        site.send({
            url: url.checkPhoneCaptcha,
            data: {
                phone: phone,
                captcha: captcha
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    constant.fn.Message('T', data.msg)
                    $('.enter_psw').hide();
                    $('.reset_psw').show();
                }
            }
        })
    }

    $(".enterPsw-btn").on('click', function () {
        //输入验证码
        enterPsw();
    })

    function resetPsw() {
        var phone = form.pswphonenum;
        var password = $(".resetPsw-phoneNum").val();
        var repassword = $('.resetPsw-inputCode').val();
        if (!password) {
            constant.fn.Message('I', "请输入密码")
        }
        if (!repassword) {
            constant.fn.Message('I', "请再次输入密码")
        }
        if (password != repassword) {
            constant.fn.Message('I', "两次输入密码不同")
        }

        site.send({
            url: url.resetPassword,
            data: {
                phone: phone,
                password: password
            },
            callback: function (data) {
                data = JSON.parse(data);
                if (data.type == '1') {
                    constant.fn.Message('T', data.msg)
                    $('.reset_psw').hide();
                    $('.reset_success').show();

                    var time = $(".RintervalSecond");
                    c = setInterval(function () {
                        if (parseInt(time.text()) > 1) {
                            time.text(parseInt(time.text()) - 1);
                        }
                        else {
                            clearInterval(c);
                        }
                    }, 1000)

                    constant.fn.Message("T", data.msg)
                    setTimeout(function () {
                        location.reload();
                    }, 6000)
                }
                else {
                    constant.fn.Message('F', data.msg)
                }
            }
        })
    }

    $(".resetPsw-btn").on('click', function () {
        //重置密码
        resetPsw();
    })
});